/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.data.hibernate;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import cassys.data.UserDAO;
import cassys.domain.user.User;
import cassys.domain.user.UserRoleMapping;
import java.util.List;
import org.hibernate.Transaction;

/**
 *
 * @author UserRoleMapping
 */
public class UserHibernateDAO extends GenericHibernateDAO<User, Long>
        implements UserDAO {

    @Override
    public List<User> getUser(String userId) throws Exception {
        try {
            Criteria query = super.getSession().createCriteria(User.class)
                    .add(Restrictions.eq("userId", userId))
                    .add(Restrictions.eq("delInd", 0));
            /*if(userId!=null && !userId.equals("") ){
                    query.add(Restrictions.eq("userId", userId));
            }*/
             
            List<User> userList = query.list();
            if (userList.isEmpty()){
                userList.add(new User());
            }
            return userList;
        } catch (Exception e) {
            
            System.out.println(e.getMessage());
            System.out.println("DB not Exists");
            e.printStackTrace();
            throw e;
          //  throw new HibernateAccessLayerException(t.toString());
        }
    }

    @Override
    public void uploadCSV(User user) throws Exception {

        //String hql = "FROM USER WHERE userId = :userId and password = :password";

        try {
            super.getSession().save(user);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("DB not Exists");
            e.printStackTrace();
            throw e;
            //  throw new HibernateAccessLayerException(t.toString());
        }


    }
    @Override
    public void saveUser(User userDTO) throws Exception {

        //String hql = "FROM USER WHERE userId = :userId and password = :password";

        try {
            super.getSession().save(userDTO);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("DB not Exists");
            e.printStackTrace();
            throw e;
            //  throw new HibernateAccessLayerException(t.toString());
        }


    }

    @Override
    public void modifyUser(User user,String role) throws Exception {
        try {
            long userSeqNo = user.getUserSeqNo();
            
            Transaction trans = super.getSession().beginTransaction();
            
            
            super.getSession().saveOrUpdate(user);
            if(userSeqNo == 0){
                UserRoleMapping userRoleMapping = new UserRoleMapping();
                userRoleMapping.setRole(role);
                userRoleMapping.setUserSeqNo(user);
                 super.getSession().save(userRoleMapping);
            }
            
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("DB not Exists");
            e.printStackTrace();
            throw e;
            //  throw new HibernateAccessLayerException(t.toString());
        }
    }
    
    @Override
    public void deleteUser(int userSeqNo) throws Exception {
        try {
            User user = new User();
            user.setUserSeqNo(userSeqNo);
            Transaction trans = super.getSession().beginTransaction();
            super.getSession().delete(user);
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("DB not Exists");
            e.printStackTrace();
            throw e;
            //  throw new HibernateAccessLayerException(t.toString());
        }
    }
}
